package com.vrv.app.cyberspace.modules.sys.controller;


import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.vrv.app.cyberspace.common.utils.HttpContextUtils;
import com.vrv.app.cyberspace.common.utils.JSONResult;
import com.vrv.app.cyberspace.common.validator.Assert;
import com.vrv.app.cyberspace.modules.sys.entity.RoleTable;
import com.vrv.app.cyberspace.modules.sys.entity.UserTable;
import com.vrv.app.cyberspace.modules.sys.service.IRoleTableService;
import com.vrv.app.cyberspace.modules.sys.service.IUserTableService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

import static com.vrv.app.cyberspace.common.utils.JSONResult.renderError;
import static com.vrv.app.cyberspace.common.utils.JSONResult.renderSuccess;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author sm
 * @since 2017-12-15
 */
@Api(description = "用户")
@RestController
@RequestMapping("/sys/user")
public class UserTableController extends BaseController {

    @Autowired
    private IUserTableService userTableService;
    @Autowired
    private IRoleTableService roleTableService;

    @ApiOperation(value = "获取用户")
    @GetMapping("/{id}")
    public JSONResult getUser(@PathVariable String id) {
        Assert.isBlank(id, "id不能为空");
        UserTable userTable = userTableService.selectById(id);
        if (userTable == null) {
            return renderError("操作被禁止，此用户信息未同步到平台！");
        }
        HttpContextUtils.getHttpServletRequest().getSession().setAttribute("user", userTable);
        RoleTable rt = roleTableService.selectById(userTable.getRoleID());
        userTable.setRole(rt);
        return renderSuccess(userTable);
    }

    @ApiOperation(value = "获取用户列表")
    @GetMapping()
    public JSONResult getListUser(Integer roleID) {
        Assert.isNull(roleID, "roleID不能为空");
        UserTable userTable = (UserTable) HttpContextUtils.getHttpServletRequest().getSession().getAttribute("user");
        EntityWrapper<UserTable> ew = new EntityWrapper<>();
        ew.eq("roleID", roleID);
        ew.and("linkdoodID!={0}", userTable.getLinkdoodID());
        List<UserTable> uts = userTableService.selectList(ew);
        for (UserTable ut :
                uts) {
            RoleTable rt = roleTableService.selectById(ut.getOrgID());
            ut.setRole(rt);
        }
        return renderSuccess(uts);
    }
}

